package com.macro.mall.tiny.modules.oms.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.macro.mall.tiny.component.wx.WxClient;
import com.macro.mall.tiny.component.wx.entity.bo.WxBO;
import com.macro.mall.tiny.modules.oms.service.OmsOrderService;
import com.macro.mall.tiny.modules.oms.service.OmsSettleApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 支付管理Controller
 * @author Tyrone
 * @date 2024/10/23 22:00
 */
@Api(tags = "OmsPayController", description = "支付管理")
@Tag(name = "OmsPayController", description = "支付管理")
@RequestMapping("/oms/pay")
@RestController
public class OmsPayController {
    private static final Logger log = LoggerFactory.getLogger(OmsPayController.class);

    @Autowired
    private WxClient wxClient;

    @Autowired
    private OmsOrderService omsOrderService;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private OmsSettleApplyService omsSettleApplyService;



    /**
     * WxBO(sessionKey=null, openid=null, unionid=null, success=true, errorMsg=null, purePhone=null, appId=null, timeStamp=null, nonceStr=null, packageValue=null, signType=null, paySign=null, wxPayBO=WxPayBO(openId=onXRu7ZFQTn8IfD9WNvUTArpLuQ8, desc=null, innerOrderNo=null, total=null, tradeState=SUCCESS, successTime=Mon Oct 28 14:00:39 CST 2024))
     * @param data
     * @return
     * @throws WxPayException
     */
    @ApiOperation(value = "支付回调通知处理")
    @PostMapping("/notify/order")
    public String parseOrderNotifyResult(@RequestBody String data) throws WxPayException {

        WxBO wxBO = wxClient.parseOrderNotifyResult(data);
        log.info("支付回调通知处理: {}", wxBO);
        threadPoolTaskExecutor.execute(()->{
            omsOrderService.handlePayResult(wxBO.getWxPayBO());
        });
        return WxPayNotifyResponse.success("成功");
    }

    /**
     * {
     *   "id": "47f02659-5883-5c2b-b979-daa4dbda6cb2",
     *   "create_time": "2024-11-30T17:19:40+08:00",
     *   "resource_type": "encrypt-resource",
     *   "event_type": "MCHTRANSFER.BATCH.FINISHED",
     *   "summary": "商家转账批次完成通知",
     *   "resource": {
     *     "original_type": "mch_payment",
     *     "algorithm": "AEAD_AES_256_GCM",
     *     "ciphertext": "FEIi4ocfjUuK+GGwOoZ+8c43VDeEujJf2zvKZ8AMI5EUef680VKNTMGM0TRxT37goPNYGeFcNCgwTMzonpXP4vI4yskzB2c3pOzNXrbearMk+VXMly0i2u3M1tpWZNcPbN/k0dZ8Onr/uJL+bVpejOb/sXbGCeCdGhFAkhcenVHMrDCDJ5XNbM48GOoAqTm7PS8Ztfn6LaZiPpGMSleh+fr9+5Q5H1T1aGJtya1wrqq9wOYQ/HqUEO1+miOMgP70hrcFPO15yFTAaOGzm3ASFf4hb0KlSGdaqFZg9/LLFAoe/gJb/D0yQTWbotnylMyOlZoTFlI+XQIKJ60RfdLha2ZkCq3qToZMy5hfFO+kTWbX/cWxbvg5DOuvuJIdVPLBhWvyxFscfupnsYi7TSXBoqFFVWeJFn2IV20Xhg==",
     *     "associated_data": "mch_payment",
     *     "nonce": "oCtJPDSWi13t"
     *   }
     * }
     * @param data
     * @return
     * @throws WxPayException
     */
    @ApiOperation(value = "转账回调通知处理")
    @PostMapping("/notify/transfer")
    public String parseTransferNotifyResult(@RequestBody String data) throws WxPayException {
        log.info("转账回调接口获取 data: {}", data);
        WxBO wxBO = wxClient.parseTransferNotify(data);
        log.info("转账回调通知处理: {}", wxBO);
        threadPoolTaskExecutor.execute(()->{
            omsSettleApplyService.handleInnerLogic(wxBO);
        });
        return WxPayNotifyResponse.success("成功");
    }




}
